const Router = require('koa-router')

const db = require('../../servse/db.js')
const tools = require('../../servse/tools.js')
const UUid = require('uuid')
const router = new Router()
const pageSize = 5

// 进入管理员用户模块
router.get('/Admin', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const currentPage = 1
  const option = await tools.getAdminList(currentPage, pageSize)
  // console.log(option)
  await ctx.render('admin', option)
})

// 进入文件管理模块
router.get('/File', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const currentPage = 1
  const option = await tools.getFileList(currentPage, pageSize)
  // console.log(option)
  await ctx.render('file', option)
})

// 进入用户管理模块
router.get('/User', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const currentPage = 1
  const option = await tools.getUserList(currentPage, pageSize)
  // console.log(option)
  await ctx.render('user', option)
})

// 进入打印机管理模块
router.get('/Print', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const currentPage = 1
  const option = await tools.getPrintList(currentPage, pageSize)
  // console.log(option)
  await ctx.render('print', option)
})

// 进入打印记录管理模块
router.get('/Record', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const currentPage = 1
  const option = await tools.getRecordList(currentPage, pageSize)
  await ctx.render('record', option)
})

// 进入后台首页
router.get('/main', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const option = await tools.ToMain()
  await ctx.render('index', option)
})

// 用户分页
router.get('/usernextpage', async (ctx, next) => {
  // console.log(ctx.query.page)
  const currentPage = parseInt(ctx.query.page)
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const option = await tools.getUserList(currentPage, pageSize)
  return ctx.render('user', option)
})

// 模糊查询用户
router.get('/findUser', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const userlist = await db.findUserLike(ctx.query.username)
  await ctx.render('user', {
    userlist: userlist,
    currentPage: 1,
    usertotalPages: Math.ceil(userlist[0].num / pageSize)
  })
})

// 模糊查询文件
router.get('/findFile', async (ctx, next) => {
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const filelist = await db.findFileLike(ctx.query.filename)
  await ctx.render('file', {
    filelist: filelist,
    currentPage: 1,
    usertotalPages: Math.ceil(filelist[0].num / pageSize)
  })
})

// 文件分页
router.get('/filenextpage', async (ctx, next) => {
  const currentPage = parseInt(ctx.query.page)
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const option = await tools.getFileList(currentPage, pageSize)
  return ctx.render('file', option)
})

// 打印机分页
router.get('/printnextpage', async (ctx, next) => {
  const currentPage = parseInt(ctx.query.page)
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const option = await tools.getPrintList(currentPage, pageSize)
  return ctx.render('print', option)
})

// 打印记录分页
router.get('/recordnextpage', async (ctx, next) => {
  const currentPage = parseInt(ctx.query.page)
  if (ctx.session.userInfo) {
    ctx.state.adminname = ctx.session.userInfo.name
  }
  const option = await tools.getRecordList(currentPage, pageSize)
  return ctx.render('record', option)
})

/**
 * @router POST admin/index/addAdmin
 * @desc 添加管理员接口
 * @access 接口公开
 */
router.post('/addAdmin', async (ctx, next) => {
  const newAdmin = ({
    adminId: UUid.v4(),
    name: ctx.request.body.name,
    password: ctx.request.body.password
  })
  // console.log(newAdmin)
  const repeat = await db.findAdminName(newAdmin.name)
  if (repeat.length > 0) {
    ctx.body = {
      message: '用户已存在'
    }
    return ctx.body
  }
  const result = await db.savaAdmin(newAdmin)
  if (result === '插入成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getAdminList(currentPage, pageSize)
    return ctx.render('admin', option)
  } else {
    ctx.body = '插入失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/updateAdmin
 * @desc 修改管理员接口
 * @access 接口公开
 */
router.post('/updateAdmin', async (ctx, next) => {
  const newAdmin = ({
    adminId: ctx.request.body.adminId,
    name: ctx.request.body.name,
    password: ctx.request.body.password
  })
  const result = await db.updateAdmin(newAdmin)
  if (result === '修改成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getAdminList(currentPage, pageSize)
    return ctx.render('admin', option)
  } else {
    ctx.body = '修改失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/update
 * @desc 修改管理员密码接口
 * @access 接口公开
 */
router.post('/update', async (ctx, next) => {
  const adminname = ctx.request.body.adminname
  const oldpassword = ctx.request.body.oldpassword
  const newpassword = ctx.request.body.newpassword
  const resultInfo = await db.findAdmin(adminname, oldpassword)
  if (resultInfo.length > 0) {
    await db.updatepassword(resultInfo[0].adminId, newpassword)
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getAdminList(currentPage, pageSize)
    await ctx.render('admin', option)
  } else {
    ctx.body = '修改失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/deleteAdmin
 * @desc 删除管理员接口
 * @access 接口公开
 */
router.post('/deleteAdmin', async (ctx, next) => {
  const adminId = ctx.request.body.adminId
  const result = await db.deleteAdmin(adminId)
  if (result === '删除成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getAdminList(currentPage, pageSize)
    await ctx.render('admin', option)
  } else {
    ctx.body = '删除失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/addPrint
 * @desc 添加打印机接口
 * @access 接口公开
 */
router.post('/addPrint', async (ctx, next) => {
  const newPrint = ({
    printerUuid: UUid.v4(),
    bid: ctx.request.body.bid,
    deviceid: ctx.request.body.deviceid,
    printerName: ctx.request.body.printerName,
    Brand: ctx.request.body.Brand,
    model: ctx.request.body.model,
    status: ctx.request.body.situation
  })
  // console.log(newPrint.status)
  const result = await db.savaPrint(newPrint)
  if (result === '插入成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getPrintList(currentPage, pageSize)
    return ctx.render('print', option)
  } else {
    ctx.body = '插入失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/updatePrint
 * @desc 修改打印机接口
 * @access 接口公开
 */
router.post('/updatePrint', async (ctx, next) => {
  const newPrint = ({
    printerUuid: ctx.request.body.printerUuid,
    bid: ctx.request.body.bid,
    deviceid: ctx.request.body.deviceid,
    printerName: ctx.request.body.printerName,
    Brand: ctx.request.body.Brand,
    model: ctx.request.body.model,
    status: ctx.request.body.situation
  })
  // console.log(newPrint)
  const result = await db.updatePrint(newPrint)
  if (result === '修改成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getPrintList(currentPage, pageSize)
    await ctx.render('print', option)
  } else {
    ctx.body = '修改失败'
  }
  return ctx.body
})

/**
 * @router POST admin/index/deletePrint
 * @desc 删除打印机接口
 * @access 接口公开
 */
router.post('/deletePrint', async (ctx, next) => {
  const printerUuid = ctx.request.body.printerUuid
  const result = await db.deletePrint(printerUuid)
  if (result === '删除成功') {
    if (ctx.session.userInfo) {
      ctx.state.adminname = ctx.session.userInfo.name
    }
    const currentPage = 1
    const option = await tools.getPrintList(currentPage, pageSize)
    await ctx.render('print', option)
  } else {
    ctx.body = '删除失败'
  }
  return ctx.body
})

module.exports = router.routes()
